package com.xsjl.article.controller;

import com.xsjl.article.pojo.Comment;
import com.xsjl.article.service.CommentService;
import com.xsjl.common.entity.Result;
import com.xsjl.common.entity.StatusCode;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.concurrent.TimeUnit;

@RestController
@CrossOrigin
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    private CommentService commentService;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping(method = RequestMethod.POST)
    public Result save(@RequestBody Comment comment) {
        // 判断用户是否登录
        Claims claims = (Claims) request.getAttribute("user_claims");
        if (claims == null) {
            return new Result(false, StatusCode.ACCESSERROR, "权限不足");
        }
        // 需要判断用户是否已经点过赞,此时需要通过token来获取用户id
        String userid = claims.getId();
        String username = claims.getSubject();
        comment.setUserid(userid);
        comment.setUsername(username);
        comment.setZan(0);
        commentService.add(comment);
        return new Result(true, StatusCode.OK, "提交成功 ");
    }

    @RequestMapping(value="/{articleid}",method= RequestMethod.GET)
    public Result findByArticleid(@PathVariable String articleid){
        return new Result(true, StatusCode.OK, "查询成功", 
                commentService.findByArticleid(articleid));
    }

    @DeleteMapping("/{id}")
    public Result remove(@PathVariable String id) {
        return new Result(true, StatusCode.OK, "删除成功");
    }

    /**
     * 评论信息点赞
     */
    @PutMapping("/thumbup/{commentId}")
    public Result thumbup(@PathVariable String commentId) {
        // 判断用户是否登录
        Claims claims = (Claims) request.getAttribute("user_claims");
        if (claims == null) {
            return new Result(false, StatusCode.ACCESSERROR, "权限不足");
        }
        // 需要判断用户是否已经点过赞,此时需要通过token来获取用户id
        String userid = claims.getId();
        //String userid="2";
        if (redisTemplate.opsForValue().get("thumbup:" + commentId + ":userId:"+ userid) != null) {
            return new Result(false, StatusCode.REPERROR, "你已经点过赞了");
        }

        redisTemplate.opsForValue().set("thumbup:" + commentId + ":userId:"+ userid, 1, getSecondNumber(), TimeUnit.SECONDS);
        commentService.addThumbup(commentId);
        return new Result(true, StatusCode.OK, "点赞成功");
    }

    //获取当前时间，到凌晨 相差的秒数
    private int getSecondNumber() {
        LocalTime midnight = LocalTime.MIDNIGHT;
        LocalDate today = LocalDate.now();
        LocalDateTime todayMidnight = LocalDateTime.of( today, midnight );
        LocalDateTime tomorrowMidnight = todayMidnight.plusDays(1);
        long seconds = TimeUnit.NANOSECONDS.toSeconds( Duration.between( LocalDateTime.now(), tomorrowMidnight ).toNanos() );
        return (int) seconds;
    }


}
